/*
 * [JunitDBManager.java]
 *  @author	-	sushil
 *  @date	- September 25, 2007
 * Copyright (c) 2007 Takeda San Diego Inc.
 * 10410 Science Center Drive, San Diego, CA 92121
 * All rights reserved.
 * This software is the confidential and proprietary information of Takeda San Diego Inc.
 * ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with Takeda.
 */
package com.ctepl.common.db;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.MissingResourceException;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.naming.Context;
import org.apache.log4j.Logger;

/**
 * 
 * @author - Sushil
 * @date - September 25, 2007
 * @desc - This class for CreateNamingProperties and Resetting the values with
 *       null.
 */
public class JunitDBManager {

	static Logger log = Logger.getLogger(JunitDBManager.class.getName());
	
	private static String InitialContextFactory = "weblogic.jndi.WLInitialContextFactory";
	
	private static String Url = "";
	
	private static String SecurityPrincipal = "weblogic";
	
	private static String SecurityCredentials = "weblogic";
	
	//private HttpURLConnection urlC = null;

	//URL url = null;
	
	private static JunitDBManager _instance = null;
	
	private JunitDBManager(){		
	}	
	
	public static JunitDBManager getInstance(){
		if(_instance == null){
			_instance = new JunitDBManager();
			_instance.initialize();
		}		
		return _instance;
	}
	
	/**
	 * @author Rahul Choudhary
	 * @date 14 Feb 2008
	 * @throws NullPointerException
	 * <P> This function will initialize all the properties as it would have been initialized thru Servlet.
	 * This will be used by JUnit for initializing propeties..
	 */
	public void initialize() throws NullPointerException{
		try {
			String hostName = InetAddress.getLocalHost().getHostName();
			String serverName = "";
			ResourceBundle rb;
			if(hostName.startsWith("web")){
				serverName=hostName;
			} else{
				serverName= InetAddress.getLocalHost().getHostAddress();
			}
			//log.debug("Server Name------------>>>>>>>>>>>>>>>>>" + serverName);
			rb = ResourceBundle.getBundle("com.syrrx.resources.DBConnection");
			
			log
			.debug("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< ....Coming inside.... >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
	
			String serverPrefix;
			log.debug("serverName="+serverName);
			if (serverName.startsWith(rb.getString("BLRSOLARISSERVER"))) {
				serverPrefix = rb.getString("BLRSOLARISTEST");
			} else if (serverName.startsWith(rb.getString("BLRTESTSERVERSP5"))) {
				serverPrefix = rb.getString("BLRTESTSP5");
			} else if (serverName.startsWith(rb.getString("BLRTESTSERVER"))) {
				serverPrefix = rb.getString("BLRTEST");
			} else if (serverName.startsWith(rb.getString("BLRDEVSERVER"))
					|| serverName.startsWith("localhost")) {
				serverPrefix = rb.getString("BLR");
			} else {
				serverPrefix = rb.getString(serverName);
			}
			serverPrefix = serverPrefix.concat(".");
		
			log.debug("SERVER NAME =" + serverPrefix);
		
			InitialContextFactory = rb.getString(serverPrefix + "INITIAL_CONTEXT_FACTORY");
			Url = rb.getString(serverPrefix + "URL");
			SecurityPrincipal = rb.getString(serverPrefix + "SECURITY_PRINCIPAL");
			SecurityCredentials = rb.getString(serverPrefix + "SECURITY_CREDENTIALS");			

		} catch (MissingResourceException e) {
			log.error(" error message:-  " + e.toString(), e);
		} catch (UnknownHostException unKnownHost) {
			log.error("Error----->" + unKnownHost.toString());
		}
	}
	
	/**
	 * @author rahul
	 * @date 14 Feb 2008 
	 * @return props The filled properties 
	 */
	public static Properties createNamingProperties(){
		if(_instance == null){
			getInstance();
		}
		Properties props = new Properties();
		props.put(Context.INITIAL_CONTEXT_FACTORY, InitialContextFactory);
		props.put(Context.PROVIDER_URL, Url);
		props.put(Context.SECURITY_PRINCIPAL, SecurityPrincipal);
		props.put(Context.SECURITY_CREDENTIALS, SecurityCredentials);
		return props;
	}	
}
